package com.example.book_system.controller;

import com.example.book_system.constant.Constants;
import com.example.book_system.model.*;
import com.example.book_system.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping("/getBookList")
    public Result getBookListByPage(PageRequest pageRequest, HttpSession session){
        //检查登录
        /*if(session.getAttribute(Constants.SESSION_USER_KEY) == null){
            return Result.unLogin();
        }*/

        if(pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1){
            return Result.success(null);
        }

        PageResult<BookInfo> pageResult = null;

        try {
            pageResult = bookService.getBookListByPage(pageRequest);
        } catch (Exception e) {
            log.error("查询翻页信息错误, e:{}", e);
        }

        return Result.success(pageResult);

    }

    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo){
        //参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || bookInfo.getCount() == null
        || bookInfo.getStatus() == null
        || bookInfo.getPrice() == null
        || !StringUtils.hasLength(bookInfo.getPublish())){
            return "参数不合法";
        }

        try {
            bookService.addBook(bookInfo);
        } catch (Exception e) {
            log.error("添加图书失败,e{}", e);
            return e.getMessage();
        }

        return "";
    }

    @RequestMapping("/getBookById")
    public BookInfo getBookById(Integer bookId){
        if(bookId < 1){
            return null;
        }

        BookInfo ret = null;

        try {
            ret = bookService.getBookById(bookId);
        } catch (Exception e) {
            log.error("查询图书失败", e);
        }

        return ret;
    }

    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        try {
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("修改失败", e);
            return e.getMessage();
        }
    }

    @RequestMapping(value = "/batchDeleteBook", produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书,ids:{}", ids);
        if(ids.size() < 0){
            return "删除为空";
        }
        try {
            bookService.batchDeleteBook(ids);
            return "";
        } catch (Exception e) {
            log.error("批量删除失败", e);
            return "删除失败";
        }
    }
}
